package com.charmzenus.champion.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.charmzenus.champion.R;

/**
 * Android自定义标题栏
 * Created by yuandl on 2016-10-27.
 */

public class Header extends LinearLayout {
    /**
     * 标题栏的根布局
     */
    private LinearLayout ll;

    // 左边按钮的layout布局
    private LinearLayout left_button_layout;
    /**
     * 标题栏的左边按返回按钮
     */
    private TextView left_button;
    /**
     * 标题栏的右边按保存按钮
     */
    private TextView right_button;
    /**
     * 标题栏的中间的文字
     */
    private TextView title;
    /**
     * 标题栏的背景颜色
     */
    private int title_background_color;
    /**
     * 标题栏的显示的标题文字
     */
    private String title_text;
    /**
     * 标题栏的显示的标题文字颜色
     */
    private int title_textColor;
    /**
     * 标题栏的显示的标题文字大小
     */
    private int title_textSize;


    /**
     * 返回按钮的资源图片
     */
    private int left_button_imageId;

    /**
     * 是否显示返回按钮
     */
    private boolean show_left_button;


    /**
     * 右边保存按钮的资源图片
     */
    private int right_button_imageId;

    /**
     * 是否显示右边保存按钮
     */
    private boolean show_right_button;

    /**
     * 标题的点击事件
     */
    private TitleOnClickListener titleOnClickListener;

    public Header(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**加载布局文件*/
        LayoutInflater.from(context).inflate(R.layout.view_titlebar, this, true);
        ll = findViewById(R.id.ll);
        left_button_layout = findViewById(R.id.left_button_layout);
        left_button = findViewById(R.id.left_button);
        right_button = findViewById(R.id.right_button);
        title = findViewById(R.id.title);

        /*获取属性值*/
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Header);

        /*标题相关*/
        title_background_color = typedArray.getColor(R.styleable.Header_title_background, getResources().getColor(R.color.colorApp));
        title_text = typedArray.getString(R.styleable.Header_title_text);
        title_textColor = typedArray.getColor(R.styleable.Header_title_textColor, Color.BLACK);
        title_textSize = typedArray.getColor(R.styleable.Header_title_textSize, 15);


        /*返回按钮相关*/
        left_button_imageId = typedArray.getResourceId(R.styleable.Header_left_button_image, R.mipmap.arrow_left);
        show_left_button = typedArray.getBoolean(R.styleable.Header_show_left_button, true);


        /*右边保存按钮相关*/
        right_button_imageId = typedArray.getResourceId(R.styleable.Header_right_button_image, 0);
        show_right_button = typedArray.getBoolean(R.styleable.Header_show_right_button, false);

        typedArray.recycle();

        /*设置值*/

        setTitle_background_color(title_background_color);
        setTitleText(title_text);
        setTitle_textSize(title_textSize);
        setTitle_textColor(title_textColor);
        setShow_left_button(show_left_button);
        setShow_right_button(show_right_button);
        setLeft_button_imageId(left_button_imageId);


        setRight_button_imageId(right_button_imageId);
        left_button_layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleOnClickListener != null) {
                    titleOnClickListener.onLeftClick();
                }
            }
        });
        right_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleOnClickListener != null) {
                    titleOnClickListener.onRightClick();
                }
            }
        });
    }


    /**
     * 获取标题栏标题TextView
     *
     * @return TextView
     */
    public TextView getTitle() {
        return title;
    }


    /**
     * 设置返回按钮的资源图片id
     *
     * @param left_button_imageId 资源图片id
     */
    public void setLeft_button_imageId(int left_button_imageId) {
        left_button.setBackgroundResource(left_button_imageId);
    }


    /**
     * 设置是否显示返回按钮
     *
     * @param show_left_button
     */
    public void setShow_left_button(boolean show_left_button) {
        left_button_layout.setVisibility(show_left_button ? VISIBLE : INVISIBLE);
    }


    /**
     * 设置右边保存按钮的资源图片
     *
     * @param right_button_imageId
     */
    public void setRight_button_imageId(int right_button_imageId) {
        right_button.setBackgroundResource(right_button_imageId);
    }


    /**
     * 设置是显示右边保存按钮
     *
     * @param show_right_button
     */
    public void setShow_right_button(boolean show_right_button) {
        right_button.setVisibility(show_right_button ? VISIBLE : INVISIBLE);
    }


    /**
     * 设置标题背景的颜色
     *
     * @param title_background_color
     */
    public void setTitle_background_color(int title_background_color) {
        ll.setBackgroundColor(title_background_color);
    }

    /**
     * 设置标题的文字
     *
     * @param title_text
     */
    public void setTitleText(String title_text) {
        title.setText(title_text);
    }

    /**
     * 设置标题的文字颜色
     *
     * @param title_textColor
     */
    public void setTitle_textColor(int title_textColor) {
        title.setTextColor(title_textColor);
    }

    /**
     * 设置标题的文字大小
     *
     * @param title_textSize
     */
    public void setTitle_textSize(int title_textSize) {
        title.setTextSize(title_textSize);
    }

    /**
     * 设置标题的点击监听
     *
     * @param titleOnClickListener
     */
    public void setOnTitleClickListener(TitleOnClickListener titleOnClickListener) {
        this.titleOnClickListener = titleOnClickListener;
    }

    /**
     * 监听标题点击接口
     */
    public interface TitleOnClickListener {
        /**
         * 返回按钮的点击事件
         */
        void onLeftClick();

        /**
         * 保存按钮的点击事件
         */
        void onRightClick();

    }
}